<?php


namespace App\Http\Controllers\Admin\BlindDate;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

//CP
class CpController extends BaseController
{
    /**
     * @api {post} /blind-date/cp/prop 新增cp道具
     *
     * @apiGroup 相亲-最佳cp
     *
     * @apiParam {String} prop_name 道具名称，必须
     * @apiParam {Int} product_id_men 男生商品id，必须
     * @apiParam {Int} product_id_women 女生商品id，必须
     * @apiParam {Int} days 天数，必须
     * @apiParam {Int} level 等级，必须
     * @apiParam {Int} score 亲密值，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function propAdd(Request $request)
    {
        $param = $request->only('prop_name', 'product_id_men', 'product_id_women', 'days', 'level', 'score');
        $v = app('validator')->make($param,[
            'prop_name' => 'required|string|max:10',
            'product_id_men' => 'required|integer|min:1',
            'product_id_women' => 'required|integer|min:1',
            'days' => 'required|integer|min:1',
            'level' => 'required|integer|min:1',
            'score' => 'required|integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        app('repo_blind_date')->addOrEditCpProp($param);
        return Response::sendData([]);
    }

    /**
     * @api {put} /blind-date/cp/prop 编辑cp道具
     *
     * @apiGroup 相亲-最佳cp
     *
     * @apiParam {String} id 序号，必须
     * @apiParam {String} prop_name 道具名称，必须
     * @apiParam {Int} product_id_men 关联的男生商品id，必须
     * @apiParam {Int} product_id_women 关联的女生商品id，必须
     * @apiParam {Int} days 天数，必须
     * @apiParam {Int} level 等级，必须
     * @apiParam {Int} score 亲密值，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function propEdit(Request $request)
    {
        $param = $request->only('id', 'prop_name', 'product_id_men', 'product_id_women', 'days', 'level', 'score');
        $v = app('validator')->make($param,[
            'id' => 'required|integer|min:1',
            'prop_name' => 'required|string|max:10',
            'product_id_men' => 'required|integer|min:1',
            'product_id_women' => 'required|integer|min:1',
            'days' => 'required|integer|min:1',
            'level' => 'required|integer|min:1',
            'score' => 'required|integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_blind_date')->addOrEditCpProp($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '编辑失败');
    }

    /**
     * @api {get} /blind-date/cp/prop cp道具列表
     *
     * @apiGroup 相亲-最佳cp
     *
     * @apiParam {Int} page 第几页，默认1
     * @apiParam {Int} page_size 每页显示条数，默认10
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "id": 1, 序号
    "name": "tmd", 名称
    "product_id_men": 1,
    "product_id_women": 1,
    "days": 1, 天数
    "level": 0, 等级
    "score": 3000, 心动总值
    "picture_men": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/c0de0f8aea46a81f770c29f4d5bd8f67C3PKL.png", 男生图片
    "special_men": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/e833ee64b2fc6552995867de3c0b9fcaLr74t.png", 男生特效
    "picture_women": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/c0de0f8aea46a81f770c29f4d5bd8f67C3PKL.png", 女生图片
    "special_women": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/e833ee64b2fc6552995867de3c0b9fcaLr74t.png" 女生特效
    }
    ],
    "first_page_url": "http://local.qipaoadmin/blind-date/cp/prop?page=1",
    "from": 1,
    "last_page": 3,
    "last_page_url": "http://local.qipaoadmin/blind-date/cp/prop?page=3",
    "next_page_url": "http://local.qipaoadmin/blind-date/cp/prop?page=2",
    "path": "http://local.qipaoadmin/blind-date/cp/prop",
    "per_page": "1",
    "prev_page_url": null,
    "to": 1,
    "total": 3
    }
    }
     */
    public function propList(Request $request)
    {
        $param = $request->only('page', 'page_size');
        $v = app('validator')->make($param,[
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $pageSize = $param['page_size'] ?? 10;

        $list = app('model_game_dating_cp_head_picture')
            ->select('id', 'name', 'product_id_men', 'product_id_women', 'days', 'level', 'score', 'create_time')
            ->where('is_delete', 0)
            ->paginate($pageSize)->toArray();

        //批量查询商品信息
        $productIdsMen = array_column($list['data'], 'product_id_men');
        $productIdsWomen = array_column($list['data'], 'product_id_women');
        $productIds = array_unique(array_merge($productIdsMen, $productIdsWomen));
        $products = app('model_mall_products')
            ->select('id', 'picture', 'special')
            ->whereIn('id', $productIds)->get()->toArray();
        $products = array_column($products, null, 'id');

        foreach ($list['data'] as &$v) {
            if (isset($products[$v['product_id_men']])) {
                $v['picture_men'] = $products[$v['product_id_men']]['picture'];
                $v['special_men'] = $products[$v['product_id_men']]['special'];
            } else {
                $v['picture_men'] = '';
                $v['special_men'] = '';
            }
            if (isset($products[$v['product_id_women']])) {
                $v['picture_women'] = $products[$v['product_id_women']]['picture'];
                $v['special_women'] = $products[$v['product_id_women']]['special'];
            } else {
                $v['picture_women'] = '';
                $v['special_women'] = '';
            }
            $v['create_time'] = empty($v['create_time']) ? '' : date('Y-m-d H:i:s', $v['create_time']);
        }
        return Response::sendData($list);
    }

    /**
     * @api {delete} /blind-date/cp/prop 删除cp道具
     *
     * @apiGroup 相亲-最佳cp
     *
     * @apiParam {String} ids 要删除的记录的id，多个用英文逗号连接
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": {
    "deleted_count": 1 删除成功的条数
    }
    }
     */
    public function propDel(Request $request)
    {
        $ids = $request->ids;

        if (empty($ids)) throw new BasicException(0, '请选择要删除的记录');

        $ids = explode(',', $ids);

        $res = app('model_game_dating_cp_head_picture')->whereIn('id', $ids)->delete();
        if ($res) {
            return Response::sendData(['deleted_count' => $res]);
        }
        throw new BasicException(0, '删除失败');
    }
}